import { defineStore } from 'pinia';
import { computed, reactive } from "vue";
export const useStore = defineStore('user', () => {
  // 微信用户信息
  const user_info = reactive({
    // 微信openid
    open_id: '',
    // 微信头像
    avatarUrl: '',
    // 微信昵称
    nickName: '',
  })
  // 登录信息
  const login_info = reactive({
    // 登录令牌
    token: '',
    // 用户id
    uid: '',
  })
  // 用户存在open_id
  const has_open_id = computed(() => {
    return !!user_info.open_id;
  })
  // 用户未登录
  const notLogin = computed(() => {
    return !(login_info.token && login_info.uid);
  })
  // 写入微信用户信息
  const setUserInfo = (e: { open_id: string, avatarUrl: string, nickName: string }) => {
    user_info.open_id = e.open_id;
    user_info.avatarUrl = e.avatarUrl;
    user_info.nickName = e.nickName;
  }
  // 写入登录信息
  const setLoginInfo = (e: { token: string }) => {
    login_info.token = e.token;
  }
  return {
    user_info,
    login_info,
    setUserInfo,
    setLoginInfo,
    has_open_id,
    notLogin,
  }
}, {
  persist: {
    enabled: true,
    strategies: [{
      key: process.env.VUE_APP_ENV + "user",
      storage: localStorage,
    }]
  }
} as any)